<!--
 * @Author: your name
 * @Date: 2021-06-07 13:51:37
 * @LastEditTime: 2021-06-10 15:09:07
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \bazai_ebook\src\views\ebook\index.vue
-->
<template>
  <div class="ebook" ref="ebook">
    <!-- loading... -->
    <ebook-header />
    <ebook-title />
    <ebook-reader />
    <ebook-menu />
    <ebook-bookmark />
    <ebook-footer />
  </div>
</template>

<script>
import { ebookMixin } from '@/mixins/mixin'
import { getReadTime, saveReadTime } from '@/utils/localStorage'
// import { px2rem } from '@/utils/utils'
export default {
  mixins: [ebookMixin],
  name: 'ebook',
  data () {
    return {
      task: null
    }
  },
  methods: {
    // 记录阅读时间
    startLoopReadTime () {
      let readTime = getReadTime(this.fileName)
      if (!readTime) {
        readTime = 0
      }
      this.task = setInterval(() => {
        readTime++
        if (readTime % 30 === 0) {
          saveReadTime(this.fileName, readTime)
        }
      }, 1000)
    },
    move (v) {
      // 向下拉动屏幕时，组件随之移动
      this.$refs.ebook.style.top = v + 'px'
    },
    restore () {
      // 将组件还原回原位
      this.$refs.ebook.style.top = 0
      // 设置过渡动画，产生回弹效果
      this.$refs.ebook.style.transition = 'all .2s linear'
      setTimeout(() => {
        // 200毫秒动画结束后，将过渡动画关闭，否则在下拉时也会产生过渡动画
        this.$refs.ebook.style.transition = ''
      }, 200)
    }
  },
  mounted () {
    // 开启记录阅读时间的定时任务
    this.startLoopReadTime()
  },
  beforeUnmount () {
    if (this.task) {
      // 关闭定时任务
      clearInterval(this.task)
    }
  },
  watch: {
    // 监听用户下拉屏幕时滚动条的y轴数值
    offsetY (v) {
      // 判断如果菜单栏没有处于显示状态（如果菜单栏显示，优先响应菜单栏事件）
      // 并且电子书已经解析完毕（未解析完毕时无法获取currentLocation
      if (!this.menuVisible && this.bookAvailable) {
        if (v > 0) {
          // 向下拉动屏幕时，调用move方法
          this.move(v)
        } else if (v === 0) {
          // y轴为0时，调用restore方法让屏幕回弹，松手时屏幕是无法自动回弹的，必须自己实现
          this.restore()
        }
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.ebook {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
</style>
